package nec.aagent.core.gcm;

import nec.aagent.B;
import a.thread.WakefulIntentService;
import android.content.Intent;

import com.google.android.gcm.GCMRegistrar;

/**
 * Register regId with the App Engine server.
 * 
 * @author tqthe
 * 
 */
public class DeviceRegisterService extends WakefulIntentService {

    public DeviceRegisterService() {
        super("DeviceRegisterService");
    }

    @Override
    protected void doHandleIntent(Intent intent) {
        String mRegId = intent.getStringExtra(B.REG_ID);
        boolean registered =
                ServerUtils.register(getApplicationContext(), mRegId);
        // At this point all attempts to register with the app
        // server failed, so we need to unregister the device
        // from GCM - the app will try to register again when
        // it is restarted. Note that GCM will send an
        // unregistered callback upon completion, but
        // GCMIntentService.onUnregistered() will ignore it.
        if (!registered) {
            GCMRegistrar.unregister(getApplicationContext());
        }
    }

}
